--[[
Global values setuped from C++:
file_name : defines current file name
]]--

------------------------------------------------------------
-- Global variables/values
------------------------------------------------------------


-- Will search config file which starts with dot '.' and rest is the same as 
-- parsed file (file_name)
dot_configs = "yes"

-- Will search config file defined in recursive_file within each directory
recursive = "yes"
recursive_file = ".plugin.lua"

PageWidth = 570
--PageWidth = 600
PageHeight= 760
--PageHeight= 800
PageCacheSize = 10
brightness = 100
contrast = 100

PreRender = 1

-- Debug file, setup it to nil to skip debug output
DebugFile="/root/TextPlugin.debug"
-- Doc type, possible values are: Unknow, FixLayout and FluidLayout
DocType = "FixLayout"
--DocType = "FluidLayout"
------------------------------------------------------------
-- Text styles/headers
------------------------------------------------------------
-- Text style list
text_styles = {}

-- Registration function
function register_style(new_style)
   local l = text_styles
   while l.item do
      if (l.item.name == new_style.name) then
         error("Such text style already registered")
      end
      l = l.next
   end
   
   text_styles = {
      next = text_styles, 
      item = new_style
   }
end   

-- Main text style: Paragraph
paragraph = {
   -- text style name
   name = "Paragraph",
   -- font name
   font = "arial",
   -- font file
   font_file = "/root/fonts/truetype/arial.ttf",
   -- font style
   style = "regular",
   -- font size
   size= 14,
   interwordspace = 0,
   -- empty line size beween text lines
   interlinespace = 5,
   -- use hyphenation or not
   hyphenation = "yes",
   -- space before style
   before = 5,
   -- space after style
   after = 5,
   -- first line identation 
   firstline = 0,
   spacing = 2,
   -- tb size (spaces)
   tabs = 8,
   -- defines how much sequential spaces will be replaced by one 
   -- (tabs doesnt cout)
   scipspaces = 0, 

   -- Create new one style
   new = function(self, style_name, o)
            o = o or {}
            setmetatable(o, self)
            self.__index = self --paragraph
            o.name = style_name

            register_style(o)

            return o
         end
}

-- All styles must be registered to allow their usage
register_style(paragraph)

------------------------------------------------------------
-- Charsets
------------------------------------------------------------

-- Charset on device
default_charset = {
   name = "WINDOWS-1251",
   mode = "force" -- could be : forced, auto, default
}

------------------------------------------------------------
-- Profiles(file styles)
------------------------------------------------------------

-- Prifiles list
profile_list= {}

-- function for registering new profiles
function register_profile(new_profile)
   local l = profile_list
   while l.item do
      if (l.item.name == new_profile.name) then
         error("Such profile already registered")
      end
      l = l.next
   end
   
   profile_list = {next = profile_list, item = new_profile}
end   

-- Main and default profile
profile = {
   ident = "^(%s+)",
   on_match = header,
   non_match = paragraph,
   name = "Base",
   background = nil,
   color = nil,
--   width = 40,
   prev_style = nil, -- previously used style
   curr_style = nil, -- current style
   charset = default_charset,

   -- create new profile
   new = function(self, profile_name, o)
            o = o or {}
            setmetatable(o, self)
            self.__index = self
            o.name = profile_name
            return o
         end,

   set_style = function(self, to)
                  self.prev_style, self.curr_style = self.curr_style, to
               end,

   line = function(self, str)
             if (not self.ident) then
                self.ident = "(%s*)"
             end
             
             local v = string.find(str, self.ident)
             if (v) then
                self:set_style(on_match)
             else
                self:set_style(non_match)
             end

             return 0, self.curr_style
          end,

   --- Do a transformation 
   --- str - string to transform
   transform = function(self, str)
                  local v, _ = string.gsub(str, "\n", " ")
                  v, _ = string.gsub(v, "^(%s+)", "")
                  return v
               end

}


------------------------------------------------------------
-- default profile
------------------------------------------------------------
default = profile

-- Deault profile must be registered via this function
function register_default(def_profile)
   if default ~= def_profile then
      default = def_profile
      
      local l = profile_list
      while l.item do
         if (l.item.name == def_profile.name) then
            return
         end
         l = l.next
      end
      
      profile_list = {next = profile_list, item = def_style}
      
   end
end


------------------------------------------------------------
-- Text styles definition
------------------------------------------------------------
header = paragraph:new("Header",
                       {
                          font="Times New Roman",
                          font_file = "/root/fonts/truetype/timesbd.ttf",
                          style="bold",
                          size=16,
                          hyphenation= "no",
                          before = 5,
                          after = 5,
                          tabs = 2
                       })

header2 = paragraph:new("H2")
header2.size = 14
header2.style="bold"

------------------------------------------------------------
-- File profiles
------------------------------------------------------------
RFC = profile:new(
                  "RFC", 
                  {
                     spaces = 4,
                     ident = "^(%w+)"
                  }
               )
RFC.p_ident = string.format("^(%"..RFC.spaces.."s)", " ")
--RFC.spaces = 4
--RFC.p_ident = string.format("^(%"..RFC.spaces.."s)", " ")
--RFC.ident = "^(%w+)"

register_profile(RFC)

Text = profile:new("Text")

register_profile(Text)
register_default(Text)


------------------------------------------------------------
--
-- USAGE example
--
------------------------------------------------------------
--[[
--
--- EXAMPLE 
--

print(file_name)

s1 = "    Starts with spaces"
s2 = "Starts with words, bla-bla-bla"
s3 = "   "

s4 = "    This is example\nwith some new line\n"

print("style_list", profile_list)
print("style_list.item", profile_list.item)

local l = profile_list
while l.item do
   print(l.item.name)
   r, t = l.item:line(s1)
   if r > 0 then
      print("Need more",t.num," lines")
      r, t = l.item:line(s3)
      print(t.name.."->","["..s1.."]")
      --       print("Font style->"..t.style)
   else
      print(t.name.."->","["..s1.."]")
      --       print("Font style->"..t.style)
      r, t = l.item:line(s2)
   end
   print(t.name.."->","["..s2.."]")
   --    print("Font style->"..t.style)
   print("Tranformed:", l.item:transform(s4))
   l = l.next
end

print ("text styles")

local l = text_styles
while l.item do
   print(l.item.name)
   print(l.item.style)
   l = l.next
end

]]--

--[[
Within C++ all above used as:
t = default_style:line(line)
printed_line = default_style:transform(line)

First line used to get font type, second one to get printable 
line: line were will be sckipped all unused symbols (spaces at 
                                                     line's begining, \n etc)
]]--
